package com.laihuichuxing.order.concurrent;
 /**
  * Description: 队列满之后的拒绝策略
  * Author: Qinxuefeng
  * Version: 1.0
  * Create Date Time: 2018/10/15 17:26.
  * Update Date Time:
  */
@FunctionalInterface
public interface DenyPolicy {
	//当队列中runnable达到limit上限时，做的策略
	void reject(Runnable runnable, ThreadPool threadPool);

	//该拒绝策略会直接将任务丢弃
	class DiscardDenyPolicy implements DenyPolicy{
		@Override
		public void reject(Runnable runnable, ThreadPool threadPool) {
			// do nothing

		}
	}

	//该拒绝策略会向任务提交者抛出异常
	class AbortDenyPolicy implements DenyPolicy{
		@Override
		public void reject(Runnable runnable, ThreadPool threadPool) {
			throw new RunnableDenyException("The runable"+runnable+"will be abort");
		}
	}

	//该策略会使任务提交者所在的线程中执行任务
	class RunnerDenyPolicy implements DenyPolicy{
		@Override
		public void reject(Runnable runnable, ThreadPool threadPool) {

			if(!threadPool.isShutdown()) {
				runnable.run();
			}

		}

	}
	
	
}
